package com.typy.www.tpay.ui.acitivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.base.BaseBackActivity;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.dialog.SyDialogHelper;
import com.typy.www.tpay.ui.dialog.SyMessageDialog;
import com.typy.www.tpay.util.CheckUtil;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.ClickFastUtil;
import com.typy.www.tpay.util.MoneyTextWatcher;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.PwdDailogUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UiHelper;
import com.typy.www.tpay.util.inter.InputCallBack;
import com.typy.www.tpay.util.listviewanimations.IntentUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 13799 on 2018/6/18.
 */

public class ActivicationActivity extends BaseBackActivity {

    public static ActivicationActivity instance;
    @BindView(R.id.tv_phone)
    AutoCompleteTextView tvPhone;
    @BindView(R.id.publish)
    TextView publish;
    @BindView(R.id.et_zpaycount)
    AutoCompleteTextView etZpaycount;
    @BindView(R.id.ll_zpay)
    PercentLinearLayout llZpay;
    @BindView(R.id.et_address)
    AutoCompleteTextView etAddress;
    @BindView(R.id.ll_address)
    PercentLinearLayout llAddress;
    @BindView(R.id.img_div)
    ImageView imgDiv;
    @BindView(R.id.et_xiane)
    AutoCompleteTextView etXiane;
    @BindView(R.id.ll_xiane)
    PercentLinearLayout llXiane;
    @BindView(R.id.et_danjia)
    AutoCompleteTextView etDanjia;
    @BindView(R.id.ll_danjia)
    PercentLinearLayout llDanjia;
    @BindView(R.id.et_shuliang)
    AutoCompleteTextView etShuliang;
    @BindView(R.id.ll_shuliang)
    PercentLinearLayout llShuliang;
    @BindView(R.id.ll_sell)
    PercentLinearLayout llSell;
    @BindView(R.id.ll_zpayhold)
    PercentLinearLayout llZpayhold;
    @BindView(R.id.et_icoshuliang)
    AutoCompleteTextView etIcoshuliang;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_heji)
    TextView tvHeji;
    @BindView(R.id.ll_icobuy)
    PercentLinearLayout llIcobuy;
    @BindView(R.id.tv_zbaoturnout)
    TextView tvZbaoturnout;
    @BindView(R.id.et_zbaoturnout)
    AutoCompleteTextView etZbaoturnout;
    @BindView(R.id.ll_zbaoturnout)
    PercentLinearLayout llZbaoturnout;
    private int classType;
    private String name;
    private int xiane;
    private String danjia;
    private String typeName;
    private String typeId;
    private String fdId;
    private double price;
    private double keyong;
    private int limtCount;
    private String strPtr1 = "";
    private String strPtr2 = "";
    private String strPtr3 = "";


    public static void show(Context context, int classType) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        IntentUtil.intent2Activity(context, intent, ActivicationActivity.class);
    }

    public static void show(Context context, int classType, double keyong) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        intent.putExtra("keyong", keyong);
        IntentUtil.intent2Activity(context, intent, ActivicationActivity.class);
    }

    public static void show(Context context, int classType, double price, int limtCount, String fdId) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        intent.putExtra("price", price);
        intent.putExtra("limtCount", limtCount);
        intent.putExtra("fdId", fdId);
        IntentUtil.intent2Activity(context, intent, ActivicationActivity.class);
    }

    public static void show(Context context, int classType, String name, int xiane, String danjia, String typeName, String typeId, String fdId) {
        Intent intent = new Intent();
        intent.putExtra("classType", classType);
        intent.putExtra("name", name);
        intent.putExtra("xiane", xiane);
        intent.putExtra("danjia", danjia);
        intent.putExtra("typeName", typeName);
        intent.putExtra("typeId", typeId);
        intent.putExtra("fdId", fdId);
        IntentUtil.intent2Activity(context, intent, ActivicationActivity.class);
    }

    @Override
    protected void initView() {
        super.initView();
        instance = this;
        ButterKnife.bind(this);
        classType = getIntent().getIntExtra("classType", 0);
        name = getIntent().getStringExtra("name");
        xiane = getIntent().getIntExtra("xiane", 0);
        danjia = getIntent().getStringExtra("danjia");
        typeName = getIntent().getStringExtra("typeName");
        typeId = getIntent().getStringExtra("typeId");
        fdId = getIntent().getStringExtra("fdId");
        keyong = getIntent().getDoubleExtra("keyong", 0);
        price = getIntent().getDoubleExtra("price", 0);
        limtCount = getIntent().getIntExtra("limtCount", 0);
        initTitle();
        mBtnRight.setVisibility(View.VISIBLE);
        etZpaycount.addTextChangedListener(new MoneyTextWatcher(etZpaycount));
    }

    private void initTitle() {
        setTitle(ClassHoldUtil.getClassTitle(classType));
        setListener();
        if (classType == ClassHoldUtil.Activication) {
            ((TextView) mBtnRight).setText("激活记录");
            tvPhone.setVisibility(View.VISIBLE);
            llZpayhold.setVisibility(View.GONE);
            llSell.setVisibility(View.GONE);
            llZbaoturnout.setVisibility(View.GONE);
        } else if (classType == ClassHoldUtil.TurnOut) {
            ((TextView) mBtnRight).setText("转出记录");
            tvPhone.setVisibility(View.VISIBLE);
            llZpayhold.setVisibility(View.GONE);
            llSell.setVisibility(View.GONE);
            llZbaoturnout.setVisibility(View.GONE);
        } else if (classType == ClassHoldUtil.TurnOutDigi) {
            ((TextView) mBtnRight).setText("转出记录");
            tvPhone.setVisibility(View.GONE);
            llZpayhold.setVisibility(View.VISIBLE);
            llSell.setVisibility(View.GONE);
            llZbaoturnout.setVisibility(View.GONE);
        } else if (classType == ClassHoldUtil.ZBAOCENTER) {
            tvPhone.setVisibility(View.GONE);
            llZpayhold.setVisibility(View.GONE);
            llSell.setVisibility(View.GONE);
            llZbaoturnout.setVisibility(View.VISIBLE);
            tvZbaoturnout.setText("可转入数量为"+StringUtil.subZeroAndDot(keyong+""));
        } else if (classType == ClassHoldUtil.ICOJINXING) {
            ((TextView) mBtnRight).setText("记录");
            tvPhone.setVisibility(View.GONE);
            llZpayhold.setVisibility(View.GONE);
            llSell.setVisibility(View.GONE);
            llIcobuy.setVisibility(View.VISIBLE);
            etIcoshuliang.setHint("请输入购买Zpay数量");
            tvContent.setText("1Zpay=¥" + price);
            publish.setText("确定购买");
            llZbaoturnout.setVisibility(View.GONE);
        } else if (classType == ClassHoldUtil.BUYFROM || classType == ClassHoldUtil.SELLFROM) {
            String title = "";
            if (classType == ClassHoldUtil.BUYFROM) {
                if (name.length() > 5)
                    title = name.substring(0, 5) + "...";
                else
                    title = name;
                setTitle("向" + title + "购买");
            } else {
                if (name.length() > 5)
                    title = name.substring(0, 5) + "...";
                else
                    title = name;
                setTitle("向" + title + "出售");
            }
            tvPhone.setVisibility(View.GONE);
            llZpayhold.setVisibility(View.GONE);
            llSell.setVisibility(View.VISIBLE);
            etXiane.setText(xiane + "");
            etDanjia.setText(danjia);
            publish.setText("确定");
            llZbaoturnout.setVisibility(View.GONE);
        }
    }

    private void setListener() {
        etIcoshuliang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtil.isEmpty(charSequence.toString()))
                    strPtr1 = charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!StringUtil.isEmpty(editable.toString()) && !editable.toString().equals(strPtr1)) {
                    int shuliang = Integer.valueOf(editable.toString());
                    if (shuliang > limtCount) {
                        shuliang = limtCount;
                    }
                    etIcoshuliang.setText(shuliang + "");
                    etIcoshuliang.setSelection(etIcoshuliang.getText().length());
                    double heji = ((double) shuliang) * price;
                    String content = "合计：¥" + heji;
                    tvHeji.setText(content);
                } else if (StringUtil.isEmpty(editable.toString())) {
                    tvHeji.setText("");
                }
            }
        });
        etShuliang.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtil.isEmpty(charSequence.toString()))
                    strPtr2 = charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!StringUtil.isEmpty(editable.toString()) && !editable.toString().equals(strPtr2)) {
                    int shuliang = Integer.valueOf(editable.toString());
                    if (shuliang > xiane) {
                        etShuliang.setText(xiane + "");
                        etShuliang.setSelection(etShuliang.getText().length());
                    }
                }
            }
        });etZbaoturnout.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!StringUtil.isEmpty(charSequence.toString()))
                    strPtr3= charSequence.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!StringUtil.isEmpty(editable.toString()) && !editable.toString().equals(strPtr3)) {
                    int shuliang = Integer.valueOf(editable.toString());
                    if (shuliang > (int) keyong) {
                        etZbaoturnout.setText((int) keyong+ "");
                        etZbaoturnout.setSelection(etZbaoturnout.getText().length());
                    }
                }
            }
        });
    }

    @Override
    protected void onRightButtonClick() {
        super.onRightButtonClick();
        RecordActivity.show(getActivity(), ClassHoldUtil.classTurnRecord(classType));
    }

    @Override
    protected int getContentResId() {
        return R.layout.activity_activication;
    }


    @OnClick(R.id.publish)
    public void onViewClicked() {
        if (classType == ClassHoldUtil.TurnOut || classType == ClassHoldUtil.Activication) {
            if (ClickFastUtil.isFastClick1000() && CheckUtil.checkPhoneTrue(this, tvPhone)) {
                check();
            }
        } else if (classType == ClassHoldUtil.TurnOutDigi) {
            if (ClickFastUtil.isFastClick1000() && CheckUtil.checkText(this, "请输入转出数量", etZpaycount) && CheckUtil.checkText(this, etAddress)) {
                Icoturnoutstep1();
            }
        } else if (classType == ClassHoldUtil.BUYFROM || classType == ClassHoldUtil.SELLFROM) {
            if (ClickFastUtil.isFastClick1000() && CheckUtil.checkText(this, etShuliang)) {
                String total = Double.valueOf(danjia) * Double.valueOf(etShuliang.getText().toString()) + "";
                BuyDetailActivity.show(getActivity(), classType, name, xiane + "", etShuliang.getText().toString(), danjia, total, typeName, typeId, fdId);
            }
        } else if (classType == ClassHoldUtil.ICOJINXING) {
            if (ClickFastUtil.isFastClick1000() && CheckUtil.checkText(this, etIcoshuliang)) {
                PwdDailogUtil.show(this, new InputCallBack() {
                    @Override
                    public void onInputFinish(String result) {
                        getBuyicoData(result);
                    }
                });
            }
        }else if (classType == ClassHoldUtil.ZBAOCENTER) {
            if (ClickFastUtil.isFastClick1000() && CheckUtil.checkTextNum(this, etZbaoturnout)) {
                PwdDailogUtil.show(this, new InputCallBack() {
                    @Override
                    public void onInputFinish(String result) {
                        getZbaoturninData(result);
                    }
                });
            }
        }
    }

    private void check() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdCode", tvPhone);
        HTTPUtils.post(this, Constance.getUserinfoURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (!ret.getData().toString().equals("{}")) {
                    ActivicationSecontActivity.show(getActivity(), ret.getData().toString(), tvPhone.getText().toString(), classType);
                } else {
                    UiHelper.showToast(getActivity(), "未查询到该用户！");
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void Icoturnoutstep1() {
        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdCode", tvPhone);
        paramsUtil.put("fdQuantity", etZpaycount);
        paramsUtil.put("fdPayAddress", etAddress);
        HTTPUtils.post(this, Constance.getIcoturnoutstep1URL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                if (!ret.getData().toString().equals("{}")) {
                    ActivicationSecontActivity.show(getActivity(), ret.getData().toString(), "", classType);
                } else {
                    UiHelper.showToast(getActivity(), "未查询到该用户！");
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
                UiHelper.showToast(getActivity(), msg);
            }
        });
    }

    private void getBuyicoData(String password) {


        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdId", fdId);
        paramsUtil.put("fdCount", etIcoshuliang);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getBuyicoURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "购买成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.ICO);
                        finish();
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", msg, "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {
                        RecordActivity.show(getActivity(), ClassHoldUtil.ICO);
                        finish();
                    }
                });
            }
        });
    }  private void getZbaoturninData(String password) {


        ParamsUtil paramsUtil = new ParamsUtil();
        paramsUtil.put("fdQuantity", etZbaoturnout);
        paramsUtil.putMd5("fdPayPwd", password);
        HTTPUtils.post(this, Constance.getZbaoturninURL(), paramsUtil.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", "转入成功", "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {

                        finish();
                    }
                });
            }

            @Override
            public void onResponseErr(int code, String msg) {
                if (!Constance.WRONGPWD.equals(msg))
                    PwdDailogUtil.hide();
                SyDialogHelper.showDlg(getActivity(), "提示", msg, "确定", new SyMessageDialog.OnClickListener() {
                    @Override
                    public void onClick(SyMessageDialog dialog) {

                    }
                });
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
